package com.example.wx.persistence.DAOImpl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.wx.persistence.AccountDAO;
import com.example.wx.util.DataBaseUtil;
import com.example.wx.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class AccountDAOImpl implements AccountDAO {
    @Autowired
    private Account account;

    @Override
    public Account findAccountByUsernameAndPassword(String username, String password) {
        String request = "{\"env\":\"cloud1-0gmugs6v938df3dc\"," + "\"query\":\"db.collection(\\\"admin_info\\\").where({username:\\\""+username+"\\\",password:\\\""+password+"\\\"}).get()\"}";
        JSONArray jsonArray=DataBaseUtil.action(request).getJSONArray("data");
        if(jsonArray.size()>0){  //登陆成功
            String result = jsonArray.getString(0);
            account = JSONObject.parseObject(result,Account.class);
        }
        return account;
    }

    @Override
    public boolean findAccountByUsername(String username) {
        boolean flag =false;
        String request = "{\"env\":\"cloud1-0gmugs6v938df3dc\"," + "\"query\":\"db.collection(\\\"admin_info\\\").where({username:\\\""+username+"\\\"}).get()\"}";
        JSONArray jsonArray=DataBaseUtil.action(request).getJSONArray("data");
        if(jsonArray.size()>0){  //用户名已被注册
            flag=true;
        }
        return flag;
    }

    @Override
    public int registerNewAdmin(Account account) {
        AccountDAO accountDAO = new AccountDAOImpl();
        int flag = -1;  //用户名存在，注册失败
        if(!accountDAO.findAccountByUsername(account.getUsername())){  //可以注册
            String request="{\"env\":\"cloud1-0gmugs6v938df3dc\","+"\"query\": \"db.collection(\\\"admin_info\\\").add({data: [{ _id: \\\""+account.getId()+"\\\",username: \\\""+account.getUsername()+"\\\", passwordd: \\\""+account.getPassword()+"\\\", truename: \\\""+account.getTruename()+"\\\", id_num: \\\""+account.getId_num()+"\\\", phone_num: \\\""+account.getPhone_num()+"\\\", }]})\"}";
//            String request="{\"env\":\"cloud1-0gmugs6v938df3dc\","+"\"query\": \"db.collection(\\\"admin_info\\\").add({data: [{ _id: \\\"3\\\", username:\\\"admin3\\\", password:\\\"333\\\", truename:\\\"name3\\\", phone_num:\\\"1651651651\\\", id_num:\\\"879861513\\\" }]})\" }";
            System.out.println(JSONObject.parseObject(request).toString());
            JSONObject jsonObject=DataBaseUtil.action(request);
            if(jsonObject.getInteger("errcode")==0){
                flag = 0; //插入成功
            }else {
                flag=1; //用户名不存在，但插入失败，注册失败
            }
            System.out.println("jsonObject"+jsonObject);
        }
        System.out.println("flag:"+flag);
        return flag;
    }



    public static void main(String[] args) {
//        AccountDAO accountDAO = new AccountDAOImpl();
//        Account account = accountDAO.findAccountByUsernameAndPassword("admin1","111");
//        if(account!=null){
//            System.out.println(account.getId()+","+account.getUsername()+","+account.getPassword()+","+account.getId_num()+","+account.getPhone_num()+","+account.getTruename());
//        }else {
//            System.out.println("xxx");
//        }

        AccountDAO accountDAO = new AccountDAOImpl();
        boolean flag = accountDAO.findAccountByUsername("admin2");
        if(flag){
            System.out.println("存在");
        }else {
            System.out.println("不存在");
        }
    }
}
